<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Actions\HandleWithdraw;
use App\Http\Controllers\Controller;
use App\Models\Withdraw;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class WithdrawController extends Controller
{
    use HasResourceActions;
    private $name = '提现申请';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Withdraw);

        $grid->model()->orderBy('id', 'desc');

        $grid->id('Id');
        $grid->user()->name('微信名称');
        $grid->money('金额')->display(function ($money) {
            return $money / 100;
        });
        $grid->tele('联系方式');
        $grid->status('状态')->display(function ($status) {
            return Withdraw::statusDesc($status);
        });

        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            if ($actions->row['status'] == 10) {
                $actions->append(new HandleWithdraw($actions->getKey()));
            }
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $withdraw = Withdraw::findOrFail($id);
        $show = new Show($withdraw);

        $show->id('Id');
        $show->user('微信名称')->unescape()->as(function ($user) {
            return $user->name;
        });
        $show->money('金额')->unescape()->as(function ($money) {
            return $money / 100;
        });
        $show->tele('联系方式');
        $show->status('状态')->using($withdraw->statusDesc());

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Withdraw);

        $form->number('user_id', 'User id');
        $form->number('money', 'Money');
        $form->text('tele', 'Tele');
        $form->number('status', 'Status')->default(10);

        return $form;
    }

    public function handle($id)
    {
        $withDraw = Withdraw::find($id);
        $withDraw->status = 20;

        if ($withDraw->save()) {
            $data = [
                'status' => true,
                'message' => trans('admin.save_succeeded'),
            ];
        } else {
            $data = [
                'status' => false,
                'message' => trans('admin.save_failed'),
            ];
        }
        return response()->json($data);

    }
}
